package region

import (
	"context"

	"github.com/nacos-group/nacos-sdk-go/common/logger"
	commonSdk "github.com/wangzhe1991/grpc-sdk/pb/common"
	regionSdk "github.com/wangzhe1991/grpc-sdk/pb/region"

	"gitee.com/krio/grpc-go-server/cmd/lib"
	"gitee.com/krio/grpc-go-server/dao/model"
	"gitee.com/krio/grpc-go-server/dao/model/region"
)

type CountryDao struct {
}

func (c *CountryDao) List(ctx context.Context, req *commonSdk.EmptyRequest) (*regionSdk.CountryListResp, error) {
	var (
		err  error
		res  []*region.Country
		data []*regionSdk.CountryListResp_One
	)
	err = lib.X.Table(model.TableRegionCountry).Find(&res)
	if err != nil {
		logger.Errorf("CountryDao: %s", err)
		return nil, err
	}
	for _, v := range res {
		data = append(data, &regionSdk.CountryListResp_One{
			CnName:     v.CNName,
			EnName:     v.ENName,
			EnFullName: v.ENFullName,
			EnCode2:    v.ENCode2,
			EnCode3:    v.ENCode3,
			NumCode:    v.NumCode,
			PhoneCode:  v.PhoneCode,
		})
	}
	return &regionSdk.CountryListResp{
		Data: data,
	}, nil
}
